﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Stock
{	public partial class Disgathers : Form
	{	private ToolStripMenuItem menuItem = null;

		public Disgathers() : this(null)
		{
		}

		public Disgathers(ToolStripMenuItem menuItem)
		{	InitializeComponent();

			this.menuItem = menuItem;
			if(menuItem != null)
				menuItem.Tag = this;
		}

		private void Disgathers_FormClosed(object sender, FormClosedEventArgs e)
		{	if(menuItem != null)
				menuItem.Tag = null;
		}
//-------------------------------------------------------------------------------------------------
		private void Disgathers_Load(object sender, EventArgs e)
		{	toolStripButtonView.Enabled = false;
			toolStripButtonDelete.Enabled = false;
			toolStripRefresh_Click(toolStripButtonRefresh, EventArgs.Empty);
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
		private void toolStripRefresh_Click(object sender, EventArgs e)
		{	try
			{	disgathersTableAdapter.Fill(stock.Disgathers);
			}
			catch(SqlException eq)
			{	MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch(Exception ex)
			{	MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
//-------------------------------------------------------------------------------------------------
		private void toolStripInsert_Click(object sender, EventArgs e)
		{	Disgather disgather = new Disgather(Util.ActionType.Insert, this);
			disgather.ShowDialog(this);
		}
//-------------------------------------------------------------------------------------------------
		private void toolStripView_Click(object sender, EventArgs e)
		{	if(disgathersGridView.CurrentCell != null)
			{	Disgather disgather = new Disgather(Util.ActionType.Select, this);
				disgather.ShowDialog(this);
			}
		}
//-------------------------------------------------------------------------------------------------
		private void toolStripDelete_Click(object sender, EventArgs e)
		{	DIProject.DataSets.Stock.DisgathersRow rowToDelete;
			if(	disgathersGridView.CurrentCell != null
			&&	(rowToDelete = (DIProject.DataSets.Stock.DisgathersRow)((DataRowView)disgathersGridView.CurrentCell.OwningRow.DataBoundItem).Row) != null
			&&	MessageBox.Show
				(	this
				,	string.Format
					(	Properties.Resources.strDeleteDisgather
					,	rowToDelete.DocNumber
					,	rowToDelete.DocDate
					)
				,	Properties.Resources.strConfirmation
				,	MessageBoxButtons.YesNo
				,	MessageBoxIcon.Question
				,	MessageBoxDefaultButton.Button1
				) == DialogResult.Yes
			)
			{	try
				{	rowToDelete.Delete();
					disgathersTableAdapter.Update(rowToDelete);
				}
				catch(SqlException eq)
				{	rowToDelete.RejectChanges();
					MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch(Exception ex)
				{	rowToDelete.RejectChanges();
					MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
		private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
		{	if(clickedCell != null)
			{	viewToolStripMenuItem.Enabled = true;
				deleteToolStripMenuItem.Enabled = true;
			}
			else
			{	viewToolStripMenuItem.Enabled = false;
				deleteToolStripMenuItem.Enabled = false;
			}
		}
//-------------------------------------------------------------------------------------------------
		private DataGridViewCell clickedCell = null;
		private void disgathersGridView_MouseDown(object sender, MouseEventArgs e)
		{	clickedCell = null;
			if(e.Button == MouseButtons.Right)
			{	DataGridView.HitTestInfo hit = disgathersGridView.HitTest(e.X, e.Y);
				if(hit.Type == DataGridViewHitTestType.Cell)
					disgathersGridView.CurrentCell = clickedCell = disgathersGridView.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
			}
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
		private void disgathersGridView_CurrentCellChanged(object sender, EventArgs e)
		{	if(disgathersGridView.CurrentCell != null)
			{	toolStripButtonView.Enabled = true;
				toolStripButtonDelete.Enabled = true;
			}
			else
			{	toolStripButtonView.Enabled = false;
				toolStripButtonDelete.Enabled = false;
			}
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
		private void disgathersGridView_KeyDown(object sender, KeyEventArgs e)
		{	if(e.KeyCode == Keys.Enter)
				e.Handled = true;
		}
//-------------------------------------------------------------------------------------------------
		private void disgathersGridView_KeyPress(object sender, KeyPressEventArgs e)
		{	if(e.KeyChar == '\r' && disgathersGridView.CurrentCell != null)
				toolStripView_Click(toolStripButtonView, EventArgs.Empty);
		}
//-------------------------------------------------------------------------------------------------
		private void disgathersGridView_MouseDoubleClick(object sender, MouseEventArgs e)
		{	DataGridView.HitTestInfo hit = disgathersGridView.HitTest(e.X, e.Y);
			if(hit.Type == DataGridViewHitTestType.Cell)
			{	disgathersGridView.CurrentCell = clickedCell = disgathersGridView.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
				toolStripView_Click(toolStripButtonView, EventArgs.Empty);
			}
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
	}
}